home *** CD-ROM | disk | FTP | other *** search
/ Danny Amor's Online Library / Danny Amor's Online Library - Volume 1.iso / html / faqs / faq / forthfaq / case_endcase < prev    next >
Encoding:
Text File  |  1995-07-25  |  7.9 KB  |  231 lines

  1. Subject: Forth FAQ: CASE,OF,ENDOF,ENDCASE.  (l/m 07.Nov.93)
  2. Newsgroups: comp.lang.forth,comp.answers,news.answers
  3. From: ForthFAQ@willett.pgh.pa.us (FAQ account for comp.lang.forth)
  4. Date: 15 Dec 93 01:39:59 GMT
  5.  
  6. Archive-name: ForthFaq/CASE_ENDCASE
  7. Last-modified: 07.Nov.93
  8. Version: 1.2
  9.  
  10.  
  11.  
  12.   These are messages that I thought deserved to be preserved.  -dwp
  13.  
  14.  
  15.   Recent changes:
  16.   1993-11-07 dwp     Started change log.  Added code from Randolph Peters,
  17.                      and comments by john - Rible.
  18.  
  19.  
  20.  -----------------------
  21.  
  22.     From: eaker@ukulele.crd.ge.com (Chuck Eaker)
  23.     Subject: Re: Wanted .. CASE,OF,ENDOF,ENDCASE
  24.     Message-ID: <1992Nov25.164255.23225@crd.ge.com>
  25.     Date: 25 Nov 92 16:42:55 GMT
  26.  
  27.     In article <jax.722669998@well.sf.ca.us>,
  28.         jax@well.sf.ca.us (Jack J. Woehr) writes:
  29.     |> In <1992Nov24.101857.84@wronz.org.nz> mentink@wronz.org.nz writes:
  30.     |> 
  31.     |> 
  32.     |> 
  33.     |> >    Can anyone help with source ( masm/forth) to the CASE statement
  34.     |> >    word set. I.E .... CASE OF ENDOF ENDCASE ....
  35.     |> 
  36.     |>     Baden's CASE is in FORTH Dimensions VIII/5.
  37.     |> 
  38.     |>     Eaker, who wrote and enduring CASE construct, checks into
  39.     |> this newsgroup once and a while. Charles?
  40.     |> 
  41.     |>         =jax=
  42.     |> -- 
  43.     |>  # jax@well.{UUCP,sf.ca.us}  # #  Member  # # Vice President,       #
  44.     |>  # du!isis!koscej!jax        # # X3J14 TC # #  Forth Interest Group #
  45.     |>  # JAX on GEnie              # # for ANS  # #   P.O. Box 8231       #
  46.     |>  # SYSOP RCFB (303) 278-0364 # #  Forth   # #    San Jose CA 95155  #
  47.  
  48.     1. FIG-Forth
  49.     Here is the source for FIG-Forth published with the original article
  50.     (Forth Dimensions, Vol. II, No. 3, pp. 37-40.). The ?PAIRS word was
  51.     FIG-Forth's way of implementing a small amount of syntax checking.
  52.  
  53.      : CASE     ?COMP CSP @ !CSP 4 ; IMMEDIATE
  54.      : OF       4 ?PAIRS COMPILE OVER COMPILE = COMPILE OBRANCH
  55.             HERE 0 ,   COMPILE DROP  5 ; IMMEDIATE
  56.      : ENDOF    5 ?PAIRS COMPILE BRANCH HERE 0 ,
  57.             SWAP 2 [COMPILE] ENDIF 4 ; IMMEDIATE
  58.      : ENDCASE  4 ?PAIRS COMPILE DROP
  59.             BEGIN SP@ CSP @ = 0=
  60.               WHILE 2 [COMPILE ENDIF REPEAT
  61.             CSP ! ; IMMEDIATE
  62.  
  63.     1a. Here is additional source for FIG-Forth published in Forth
  64.     Dimensions, Vol. III, No. 6, pp. 187-188 in an article by Alfred J.
  65.     Monroe.  He adds a primitive compiled by OF which reduces the amount
  66.     of code compiled by OF. Use the definitions of CASE, ENDOF, and
  67.     ENDCASE given above.
  68.  
  69.      : (OF)     OVER = IF DROP 1 ELSE 0 ENDIF ;
  70.      : OF       4 ?PAIRS COMPILE (OF) COMPILE 0BRANCH
  71.             HERE 0 , 5 ; IMMEDIATE
  72.  
  73.     Mr. Monroe also gave code for some interesting variants:
  74.  
  75.      : (<OF)    OVER > IF DROP 1 ELSE 0 ENDIF ;
  76.      : <OF      4 ?PAIRS COMPILE (<OF) COMPILE 0BRANCH
  77.             HERE 0 , 5 ; IMMEDIATE
  78.      : (>OF)    OVER > IF DROP 1 ELSE 0 ENDIF ;
  79.      : >OF      4 ?PAIRS COMPILE (>OF) COMPILE 0BRANCH
  80.             HERE 0 , 5 ; IMMEDIATE
  81.      : RANGE    >R OVER DUP R> 1+ < IF SWAP 1- > IF DROP 1 ELSE 0
  82.             ENDIF ELSE DROP DROP 0 ENDIF ;
  83.      : RNG-OF   4 ?PAIRS COMPILE RANGE COMPILE 0BRANCH
  84.             HERE 0 , 5 ; IMMEDIATE
  85.  
  86.     1b. It is quite common to define (OF) as a CODE word and have
  87.     it combine the functions of the run-time (OF) and the compile-time
  88.     0BRANCH in the previous definitions. This reduces the amount of
  89.     compiled code even more.
  90.      CODE (OF) ( 1. Remove the top element of the stack and call it A.
  91.              2. If A equals the new top element of the stack,
  92.                 remove the new top element of the stack,
  93.                 skip over the branch vector, and execute
  94.                 the code which follows it.
  95.             Else
  96.                 continue execution at the location indicated
  97.                 by the branch vector.
  98.             ) END-CODE
  99.      : OF      4 ?PAIRS COMPILE (OF) HERE 0 , 5 ; IMMEDIATE
  100.  
  101.     2. dpANS-3
  102.     dpANS-3 contains the following definitions (p. 133) to illustrate
  103.     control structure extension. Note that it would be quite easy to
  104.     optimize OF along the lines suggested above. Note also that there is no
  105.     syntax checking.  These words may appear anywhere and not necessarily
  106.     combined with each other. In fact, ENDOF may be dispensed with entirely
  107.     and replaced with ELSE. Compile-time monitoring of the syntax of
  108.     control structure words is a perennial Forth problem.
  109.  
  110.      0 CONSTANT CASE IMMEDIATE  ( init count of OFs )
  111.  
  112.      : OF  ( #of -- orig #of+1 / x -- )
  113.         1+    ( count OFs )
  114.         >R    ( move off the stack in case the control-flow )
  115.           ( stack is the data stack. )
  116.         POSTPONE OVER  POSTPONE = ( copy and test case value )
  117.         POSTPONE IF    ( add orig to control flow stack )
  118.         POSTPONE DROP  ( discards case value if = )
  119.         R> ;           ( we can bring count back now )
  120.      IMMEDIATE
  121.  
  122.      : ENDOF  ( orig1 #of -- orig2 #of )
  123.         >R    ( move off the stack in case the control-flow )
  124.           ( stack is the data stack. )
  125.         POSTPONE ELSE
  126.         R> ;  ( we can bring count back now )
  127.      IMMEDIATE
  128.  
  129.      : ENDCASE ( orig 1..orign #of -- )
  130.         POSTPONE DROP  ( discard case value )
  131.         0 ?DO
  132.           POSTPONE THEN
  133.         LOOP ;
  134.      IMMEDIATE
  135.  
  136.     -- 
  137.     Chuck Eaker / P.O. Box 8, K-1 3C12 / Schenectady, NY 12301 USA
  138.     eaker@crd.ge.com        eaker@crdgw1.UUCP       (518) 387-5964
  139.  
  140.  
  141.  
  142.  -----------------------
  143.  
  144.     From: sorry no e-mail address (Randolph Peters)
  145.     Subject: pocket forth: case...endcase
  146.     Message-ID: <sorry?no?e-mail?address-221093104409@meded6.med.upenn.edu>
  147.     Date: 22 Oct 93 14:49:04 GMT
  148.  
  149.     What follows is code I have cobbled together to create a 
  150.     case construct in pocket forth. I have found several instances
  151.     where it would be useful to have a multi-branching if, and so
  152.     I made this one up. Since I haven't seen the FAQ on case 
  153.     endcase recently, I am providing this for the benefit of the
  154.     pocket forthers out there.
  155.  
  156.  
  157.     : case      0 ; immediate
  158.     : of
  159.       [ ' over literal ] compile
  160.       [ ' =    literal ] compile
  161.       [compile] if ; immediate  
  162.     : endof     [compile] else ; immediate
  163.     : otherwise ; immediate ( syntactic sugar)
  164.     : endcase   
  165.       begin  ?dup while [compile] then  repeat 
  166.       [ ' drop literal ] compile  ; immediate
  167.  
  168.  
  169.     ( example of its use )
  170.  
  171.     123 constant SPADE
  172.     234 constant HEART
  173.     345 constant DIAMOND
  174.     456 constant CLUB
  175.  
  176.     : ?cardtype ( n -- )
  177.       case 
  178.        spade of ." It's a spade" cr endof
  179.        heart of ." It's a heart" cr endof
  180.        diamond of ." It's a diamond" cr endof
  181.        club of ." It's a club" cr endof
  182.        otherwise ." I don't recognize this suit." 
  183.       endcase ;
  184.  
  185.     234 ?cardtype
  186.  
  187.     I found this useful in several cases, so please, no flames about
  188.     the purity or appropriateness of case...endcase in forth. 
  189.  
  190.  
  191.     Randolph M. Peters
  192.  
  193.     Standard disclaimer.
  194.  
  195.  
  196.  
  197.  -----------------------
  198.  
  199.     From: jrible@cup.portal.com (john - Rible)
  200.     Subject: Re: pocket forth: case...endcase
  201.     Message-ID: <93973@cup.portal.com>
  202.     Date: Sat, 23 Oct 93 07:16:05 PDT
  203.  
  204.     Randolph Peters submitted some code for a CASE ...  ENDCASE
  205.     structure that is significantly different from most of the
  206.     implementations that I know of.  I apologize for not copying and
  207.     changing the code directly, but Portal's online mailer is crude
  208.     when used with just Windows terminal program.
  209.  
  210.     Most implementations do a drop AFTER the 'if' in the definition of
  211.     OF, removing the case variable from the stack.  Then ENDCASE has
  212.     its drop BEFORE it resolves the forward references.  Both methods
  213.     work, but the user better know which style is implemented in
  214.     his/her system.
  215.  
  216.     -John
  217. ---
  218. If you have any questions about ForthNet/comp.lang.forth or any information
  219. to add/delete or correct in this message or any suggestions on formatting or
  220. presentation, please contact Doug Philips at one of the following addresses:
  221.           Internet: dwp@willett.pgh.pa.us
  222.           Usenet:   ...!uunet!willett.pgh.pa.us!dwp
  223.           GEnie:    D.PHILIPS3
  224.  
  225. any suggestions on formatting or
  226. presentation, please contact Doug Philips at one of the following addresses:
  227.           Internet: dwp@willett.pgh.pa.us
  228.           Usenet:   ...!uunet!willett.pgh.pa.us!dwp
  229.           GEnie:    D.PHILIPS3
  230.  
  231.